package pers.lmd.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import pers.lmd.domain.entity.Menu;
import pers.lmd.mapper.MenuMapper;
import pers.lmd.service.IMenuService;
import pers.lmd.service.IUserService;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 10149
 * @description 针对表【menu】的数据库操作Service实现
 * @createDate 2023-04-09 19:24:24
 */
@RequiredArgsConstructor
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
        implements IMenuService {

    private final IUserService userService;

    @Override
    public List<Tree<Integer>> getMenu() {
        int userRole = userService.getRole();
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        // 根据角色获取菜单
        wrapper.eq(Menu::getRid, userRole);
        List<Menu> menuList = list(wrapper);
        // 树节点配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 转换器
        List<Tree<Integer>> treeNodes = TreeUtil.build(menuList, 0, treeNodeConfig, (object, tree) -> {
            tree.setId(object.getId()); // 必填属性
            tree.setParentId(object.getParentId()); // 必填属性
            // 扩展属性 ...
            tree.putExtra("icon", object.getIcon());
            tree.setName(object.getName());
            tree.putExtra("path", object.getPath());
            tree.putExtra("component", object.getComponent());
        });
        return treeNodes;
    }

    public List<String> getPermissionByUid(BigInteger id){
        return baseMapper.getPermissionByUid(id).stream().map(Menu::getPermission).collect(Collectors.toList());
    }

}




